package com.everis.util;

//import hiper.hfirmas.util.Base16;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.imageio.stream.ImageOutputStream;

public class ImagenUtil {
	
	
	public static byte[] convert_from_StringBase64ImagePNG_to_ArrayByteTIFF(final String stringBase64ImagePNG) throws Exception {
		byte[] arrayByteTIFF = null;
		try {
			byte[] b = Base64.decode(stringBase64ImagePNG);
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			
			File file = File.createTempFile("temp.png", ".tmp");
			ImageOutputStream ios =  new FileImageOutputStream(file);
			ImageIO.write((RenderedImage)image, "tiff", ios);
			
			arrayByteTIFF = convertImageTIFF_to_ArrayByte(file);
			file.deleteOnExit();
		} catch(IOException e) {
			e.printStackTrace();
		}
		return arrayByteTIFF;
	}
	
	public static String convert_from_StringBase64ImagePNG_to_StringBase64ImageTIFF(final String stringBase64ImagePNG) throws Exception {
		String stringBase64ImageTIFF = null;
		try {
			byte[] b = Base64.decode(stringBase64ImagePNG);
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			
			File file = File.createTempFile("temp.png", ".tmp", new File("D:/prueba/"));
			ImageOutputStream ios =  new FileImageOutputStream(file);
			ImageIO.write((RenderedImage)image, "tiff", ios);
			
			stringBase64ImageTIFF = convertImageTIFF_to_StringBase64(file);
			file.deleteOnExit();
		} catch(IOException e) {
			e.printStackTrace();
		}
		return stringBase64ImageTIFF;
	}
	
	
	public static void convert_from_StringBase64ImageTIFF_to_StringBase64ImagePNG(
			final String stringBase64ImageTIFF, final String rutaImagenPNGStringBase64) throws Exception {
		try {
			byte[] b = Base64.decode(stringBase64ImageTIFF);
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			
			File file = File.createTempFile("temp.tiff", ".tmp");
			ImageOutputStream ios =  new FileImageOutputStream(file);
			ImageIO.write((RenderedImage)image, "png", ios);
			
			convertImagePNG_to_StringBase64(file, rutaImagenPNGStringBase64);
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String convert_from_StringBase64ImageTIFF_to_StringBase64ImagePNG2(
			final String stringBase64ImageTIFF, final String rutaImagenPNGStringBase64) throws Exception {
		String stringBase64ImagePNG="";
		try {
			byte[] b = Base64.decode(stringBase64ImageTIFF);
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			
			File file = File.createTempFile("temp.tiff", ".tmp");
			ImageOutputStream ios =  new FileImageOutputStream(file);
			ImageIO.write((RenderedImage)image, "png", ios);
			
			stringBase64ImagePNG=convertImagePNG_to_StringBase64_2(file, rutaImagenPNGStringBase64);
		} catch(IOException e) {
			e.printStackTrace();
		}
		return stringBase64ImagePNG;
	}
	
	public static String convert_from_ByteBCDTIFF_to_StringBase64ImagePNG(byte[] stringBase64ImageTIFF) {
		String stringBase64ImagePNG = null;
		try {
			//byte[] b = Base64.decode(stringBase64ImageTIFF);
			byte[] b = stringBase64ImageTIFF;
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			
			File file = File.createTempFile("temp.tiff", ".tmp");
			ImageOutputStream ios =  new FileImageOutputStream(file);
			ImageIO.write((RenderedImage)image, "png", ios);
			
			stringBase64ImagePNG = convertImagePNG_to_StringBase64(file);
			file.deleteOnExit();
		} catch(Exception e) {
			e.printStackTrace();
			stringBase64ImagePNG = null;
		}
		
		return stringBase64ImagePNG;
	}
	
	public static void convert_from_StringBase64ImagePNG_to_StringBase64ImageTIFF(
			final String stringBase64ImagePNG, final String rutaImagenTIFFStringBase64) throws Exception {
		try {
			byte[] b = Base64.decode(stringBase64ImagePNG);
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			File file = File.createTempFile("temp.png", ".tmp");
			ImageOutputStream ios =  new FileImageOutputStream(file);
			//ios.setByteOrder(ByteOrder.LITTLE_ENDIAN);
			ImageIO.write((RenderedImage)image, "tiff", ios);
			
			convertImageTIFF_to_StringBase64(file, rutaImagenTIFFStringBase64);
			file.deleteOnExit();
		} catch(IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void convertStringBase64ImagePNG_to_ImagePNG(final String stringBase64ImagePNG, final String rutaImagenPNG) throws Exception {
		try {
			byte[] b = Base64.decode(stringBase64ImagePNG);
			
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);
			
			ImageIO.write((RenderedImage)image, "png", new File(rutaImagenPNG));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void convertStringBase64ImageTIFF_to_ImageTIFF(final String stringBase64ImageTIFF, final String rutaImagenTIFF) throws Exception {
		try {
			byte[] b = Base64.decode(stringBase64ImageTIFF);
			
			ByteArrayInputStream bais = new ByteArrayInputStream(b);
			Image image = ImageIO.read(bais);			
			ImageIO.write((RenderedImage)image, "tiff", new File(rutaImagenTIFF));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Convierte una imagen PNG a String base 64. 
	 * 
	 * Primero lee el archivo fisico desde la ruta llamada <code>rutaImagenPNG</code>, luego
	 * escribe el <code>String</code> convertido en base 64 en la ruta llamada <code>rutaImagenStringBase64</code>.
	 * 
	 * @param rutaImagenPNG
	 * @param rutaImagenStringBase64
	 * @throws Exception 
	 */
	public static void convertImagePNG_to_StringBase64(final String rutaImagenPNG, final String rutaImagenStringBase64) throws Exception {
		File filePNG = new File(rutaImagenPNG);
		
		try {
			BufferedImage biPNG = ImageIO.read(filePNG);
			
			byte[] bytesPNG;
			ByteArrayOutputStream baosPNG = new ByteArrayOutputStream();
			ImageIO.write(biPNG, "png", baosPNG);
			baosPNG.flush();
			bytesPNG = baosPNG.toByteArray();
			baosPNG.close();
			
			String stringPNGBase64 = Base64.encodeBytes(bytesPNG);
			
			writeStringBase64ImagePNG_to_FileTXT(stringPNGBase64, rutaImagenStringBase64);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Convierte una imagen TIFF a String base 64. 
	 * 
	 * Primero lee el archivo fisico desde la ruta llamada <code>rutaImagenTIFF</code>, luego
	 * escribe el <code>String</code> convertido en base 64 en la ruta llamada <code>rutaImagenStringBase64</code>.
	 * 
	 * @param rutaImagenTIFF
	 * @param rutaImagenStringBase64
	 * @throws Exception 
	 */
	public static void convertImageTIFF_to_StringBase64(final String rutaImagenTIFF,  final String rutaImagenStringBase64) throws Exception {
		File fileTIFF = new File(rutaImagenTIFF);
		
		try {
			BufferedImage biTIFF = ImageIO.read(fileTIFF);
			
			byte[] bytesTIFF;
			ByteArrayOutputStream baosTIFF = new ByteArrayOutputStream();
			ImageIO.write(biTIFF, "tiff", baosTIFF);
			baosTIFF.flush();
			bytesTIFF = baosTIFF.toByteArray();
			baosTIFF.close();
			
			String stringTIFF = Base64.encodeBytes(bytesTIFF);
			
			writeStringBase64ImageTIFF_to_FileTXT(stringTIFF, rutaImagenStringBase64);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String convertImageTIFF_to_StringBase64(final File fileImagenTIFF) throws Exception {
		File fileTIFF = fileImagenTIFF;
		String stringTIFFBase64 = null;
		try {
			BufferedImage biTIFF = ImageIO.read(fileTIFF);
			
			byte[] bytesTIFF;
			ByteArrayOutputStream baosTIFF = new ByteArrayOutputStream();
			ImageIO.write(biTIFF, "tiff", baosTIFF);
			baosTIFF.flush();
			bytesTIFF = baosTIFF.toByteArray();
			baosTIFF.close();
			
			stringTIFFBase64 = Base64.encodeBytes(bytesTIFF);
		} catch (IOException e) {
			stringTIFFBase64 = null;
//			e.printStackTrace();
		}
		
		return stringTIFFBase64;
	}
	
	public static byte[] convertImageTIFF_to_ArrayByte(final File fileImagenTIFF) {
		File fileTIFF = fileImagenTIFF;
		//String stringTIFFBase64 = null;
		byte[] bytesTIFF=null;
		try {
			BufferedImage biTIFF = ImageIO.read(fileTIFF);
			
			ByteArrayOutputStream baosTIFF = new ByteArrayOutputStream();
			ImageIO.write(biTIFF, "tiff", baosTIFF);
			baosTIFF.flush();
			bytesTIFF = baosTIFF.toByteArray();
			baosTIFF.close();
			
			//stringTIFFBase64 = Base64.encodeBytes(bytesTIFF);
		} catch (IOException e) {
		}
		return bytesTIFF;
	}
	
	public static void convertImageTIFF_to_StringBase64(final File fileImagenTIFF,  final String rutaImagenStringBase64) throws Exception {
		try {
			BufferedImage biTIFF = ImageIO.read(fileImagenTIFF);
			
			byte[] bytesTIFF;
			ByteArrayOutputStream baosTIFF = new ByteArrayOutputStream();
			ImageIO.write(biTIFF, "tiff", baosTIFF);
			baosTIFF.flush();
			bytesTIFF = baosTIFF.toByteArray();
			baosTIFF.close();
			
			String stringTIFF = Base64.encodeBytes(bytesTIFF);
			
			writeStringBase64ImageTIFF_to_FileTXT(stringTIFF, rutaImagenStringBase64);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void convertImagePNG_to_StringBase64(final File fileImagenPNG,  final String rutaImagenStringBase64) throws Exception {
		try {
			BufferedImage biPNG = ImageIO.read(fileImagenPNG);
			
			byte[] bytesPNG;
			ByteArrayOutputStream baosPNG = new ByteArrayOutputStream();
			ImageIO.write(biPNG, "png", baosPNG);
			baosPNG.flush();
			bytesPNG = baosPNG.toByteArray();
			baosPNG.close();
			
			String stringPNG = Base64.encodeBytes(bytesPNG);
			
			writeStringBase64ImagePNG_to_FileTXT(stringPNG, rutaImagenStringBase64);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String convertImagePNG_to_StringBase64_2(final File fileImagenPNG,  final String rutaImagenStringBase64) throws Exception {
		String stringPNG ="";
		try {
			BufferedImage biPNG = ImageIO.read(fileImagenPNG);
			
			byte[] bytesPNG;
			ByteArrayOutputStream baosPNG = new ByteArrayOutputStream();
			ImageIO.write(biPNG, "png", baosPNG);
			baosPNG.flush();
			bytesPNG = baosPNG.toByteArray();
			baosPNG.close();
			
			stringPNG = Base64.encodeBytes(bytesPNG);
			
			writeStringBase64ImagePNG_to_FileTXT(stringPNG, rutaImagenStringBase64);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stringPNG;
	}
	
	public static String convertImagePNG_to_StringBase64(final File fileImagenPNG) throws Exception {
		String stringBase64ImagePNG = null;
		try {
			BufferedImage biPNG = ImageIO.read(fileImagenPNG);
			
			byte[] bytesPNG;
			ByteArrayOutputStream baosPNG = new ByteArrayOutputStream();
			ImageIO.write(biPNG, "png", baosPNG);
			baosPNG.flush();
			bytesPNG = baosPNG.toByteArray();
			baosPNG.close();
			
			stringBase64ImagePNG = Base64.encodeBytes(bytesPNG);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return stringBase64ImagePNG;
	}
	
	/**
	 * Lee el <code>String</code> en base 64 de la imagen PNG desde un archivo TXT.
	 * Parametro de entrada es la ruta de dicho archivo.
	 * Retorna la cadena recuperado desde el archivo fisico.
	 * 
	 * @param rutaStringBase64ImagePNG
	 * @return String
	 */
	public static String readStringBase64ImagePNG_from_FileTXT(final String rutaStringBase64ImagePNG) {
		File file = new File(rutaStringBase64ImagePNG);
		FileInputStream fis = null;
 
		StringBuilder sb = new StringBuilder();
		
		try {
			fis = new FileInputStream(file);
 
			int content;
			while ((content = fis.read()) != -1) {
				sb.append((char)content);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
		return sb.toString();
	}
	
	/**
	 * Lee el <code>String</code> en base 64 de la imagen TIFF desde un archivo TXT.
	 * Parametro de entrada es la ruta de dicho archivo.
	 * Retorna la cadena recuperado desde el archivo fisico.
	 * 
	 * @param rutaStringBase64ImageTIFF
	 * @return String
	 */
	public static String readStringBase64ImageTIFF_from_FileTXT(final String rutaStringBase64ImageTIFF) {
		File file = new File(rutaStringBase64ImageTIFF);
		FileInputStream fis = null;
 
		StringBuilder sb = new StringBuilder();
		
		try {
			fis = new FileInputStream(file);
			
			int content;
			while ((content = fis.read()) != -1) {
				sb.append((char)content);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null)
					fis.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
		return sb.toString();
	}
	
	public static void writeStringBase64ImagePNG_to_FileTXT(final String stringBase64ImagePNG, final String rutaDestinoArchivoTXT) {
		File file = new File(rutaDestinoArchivoTXT);
 
		try {
			FileOutputStream fop = new FileOutputStream(file, true);
			
			if (!file.exists()) {
				file.createNewFile();
			}
 
			byte[] contentInBytes = stringBase64ImagePNG.getBytes();
 
			fop.write(contentInBytes);
			fop.flush();
			fop.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void writeStringBase64ImageTIFF_to_FileTXT(final String stringBase64ImageTIFF, final String rutaDestinoArchivoTXT) {
		File file = new File(rutaDestinoArchivoTXT);
 
		try {
			FileOutputStream fop = new FileOutputStream(file);
			
			if (!file.exists()) {
				file.createNewFile();
			}
 
			byte[] contentInBytes = stringBase64ImageTIFF.getBytes();
 
			fop.write(contentInBytes);
			fop.flush();
			fop.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	} 
}
